package cn.ulyer.core.secure;

import cn.ulyer.common.security.JSONAccessDeniedHandler;
import cn.ulyer.common.security.JSONAuthenticationEntryPoint;
import cn.ulyer.common.security.authority.AuthorityProvider;
import cn.ulyer.common.security.authority.AuthorityService;
import cn.ulyer.common.security.authority.AuthorityHandlerStrategy;
import cn.ulyer.core.secure.authority.*;
import cn.ulyer.core.secure.filter.JwtFilterAuthenticationTokenFilter;
import cn.ulyer.core.secure.service.DbUserDetailService;
import cn.ulyer.core.secure.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.CorsFilter;

import java.util.List;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CorsFilter corsFilter;


    @Autowired(required = false)
    private List<AuthorityHandlerStrategy> authorityStrategies;


    @Bean
    @Primary
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    @Bean
    protected UserDetailsService userDetailsService() {
        return new DbUserDetailService();
    }


    @Override
    public void configure(WebSecurity web) {
        web.ignoring() .antMatchers("/swagger-resources/**","/webjars/**")
                .antMatchers("/v2/api-docs-ext/**","/doc.html","/v2/api-docs");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().sameOrigin();
        http.cors().and()
                .formLogin().disable().csrf().disable()
                .httpBasic().disable();
        http.authorizeRequests()
                .anyRequest().access("@authorityService.hasAuthority(request,authentication)");
        http.exceptionHandling().accessDeniedHandler(new JSONAccessDeniedHandler())
                .authenticationEntryPoint(new JSONAuthenticationEntryPoint()).and()
       .addFilterBefore(new JwtFilterAuthenticationTokenFilter(tokenService), UsernamePasswordAuthenticationFilter.class)
       .addFilterBefore(corsFilter,JwtFilterAuthenticationTokenFilter.class);
    }


    @Bean
    public AuthorityProvider authorityProvider(){
        return new CacheAuthorityProvider();
    }



    @Bean
    @Primary
    public AuthorityService authorityService(AuthorityProvider authorityProvider){
        return new UrlAuthorityService(authorityProvider, authorityStrategies);
    }






}
